<template>
	<view class="contents" :style="'height:'+screenHeight+'px;'">
		<view class="top">
			<u-subsection :list="list" v-model="current" @change="changes" mode="button" active-color="#ffffff"
				button-color="#409EFF" bg-color="#ffffff"></u-subsection>
		</view>
		<view class="" v-if="show">
			<u-collapse :arrow="false">
				<u-collapse-item v-for="(item, index) in orderlist" :key="index">
					<template v-slot:title>
						<view class="leftf">
							<view class="information">
								<view class="gray">检查项目：<span class="black">{{item.feename}}</span></view>
								<view class="gray">时间：<span class="black">{{item.datetime}}</span></view>
							</view>

							<view class="rightbutton">
								<view class="label" style="background-color: #E6A23C;" v-if="item.type==1">
									<span>门诊</span>
								</view>
								<view class="label" style="background-color: #00aaff;" v-if="item.type==2">
									<span>住院</span>
								</view>
							</view>
						</view>
					</template>
					<view class="" v-for="(item2,index) in item.list" :key="index">
						<view class="bodyy">
							<view class="gray">项目：<span class="black">{{item2.name}}</span></view>
							<view class="gray" v-if="current==0">单位：<span class="black">{{item2.dw}}</span></view>
							<view class="gray" v-if="current==0">具体数值：<span class="black">{{item2.value}}</span></view>
							<view class="gray" v-if="current==0">范围：<span class="black">{{item2.ckfw}}</span></view>
							<view class="gray" v-if="current==1">诊断描述：<span class="black">{{item2.lcsj}}</span></view>
							<view class="gray" v-if="current==1">结果：<span class="black">{{item2.lczd}}</span></view>
						</view>
					</view>
				</u-collapse-item>
			</u-collapse>


		</view>
		<view class="none" v-else>
			<view class="coent">
				<u-empty text="当前暂无记录" mode="data" icon="/static/zwnr.png" icon-size="200">
				</u-empty>
			</view>
		</view>
		<u-modal :show="deleteshow" :showCancelButton="true" :title="title" :content='content' @confirm="confirm()"
			@cancel="deleteshow=false"></u-modal>
		<u-modal :show="showdeletes" :showCancelButton="true" :title="deletetitle" :content='deletecontent'
			@confirm="deletesconfirm()" @cancel="showdeletes=false"></u-modal>
		<u-modal :show="showrefund" :showCancelButton="true" :title="refundtitle" :content='refundcontent'
			@confirm="refundconfirm()" @cancel="showrefund=false"></u-modal>






	</view>
	<u-modal    v-if="doctorshow"  v-model="newshow" title="预约挂号条款" confirm-color='#55aaff' :title-style="{ fontWeight: 'bold'}"
		confirm-text="前往就诊人管理" :show-cancel-button="false" @confirm="newconfirm" :mask-close-able="false">
		<view class="slot-content">
			
			<view class="" style="padding: 20px;">
				<view class="" style="text-align: center;">

				</view>
				<view class="">
					当前暂无就诊人，请前往就诊人管理页面添加就诊人。
				</view>
			</view>

		</view>
	</u-modal>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {

				screenHeight: getApp().globalData.screenHeight,
				list: [{
					Status: 0,
					name: '检查报告'
				}, {
					Status: 1,
					name: '检验报告'
				}],
				newshow: true,

				doctorshow: false,
				current: 0,
				showdeletes: false,
				deleteshow: false,
				showrefund: false,
				page: 1,
				rows: 20,
				Status: 'all',
				orderlist: [],
				title2: "66666",
				title: '取消预约',
				content: '取消预约之后不可恢复，是否确认',
				deletetitle: '删除预约记录',
				deletecontent: '删除预约记录之后不可恢复，是否确认',
				refundtitle: '退款',
				refundcontent: '退款之后不可恢复，是否确认',
				OrderNumber: '',
				openid: '',
				month: '',
				day: '',
				show: true
			}
		},
		computed: {
			...mapState(['token'], ['userInfo']),
			...mapGetters(['hasLogin', 'getUserInfo'])
		},
		onLoad: function(option) {
			if (this.hasLogin) {
				this.getjiuzhen()
				this.$store.dispatch('getUserInfo');
				//this.recordlist()
				if (this.current == 0) {
					this.getlis()
			
				} else if (this.current == 1) {
					this.getpacs()
				}
			} else {
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					content: '登录后才可以操作',
					confirmText: '去登录',
					showCancel: false,
					success: function(res) {
						uni.reLaunch({
							url: '/pages/auth/login?name=newbgcx'
						})
					}
				});
				return;
			}


			
		},
		onPullDownRefresh() {
			this.recordlist()
			uni.stopPullDownRefresh()
		},

		onShow() {
			
		},
		methods: {
			// gettype(text) {

			// 	if (text == 0)
			// 		return '住院'
			// 	else
			// 		return '门诊'
			// },
			detail(OrderId) {
				console.log("OrderId", OrderId)
				uni.navigateTo({
					url: `/pages/register/Scheduling/record/recorddetail?OrderId=${OrderId}`,
					fail(error) {
						console.log(error)
					}
				})
			},
			newconfirm() {

				// 3秒后自动关闭
				// this.doctorshow = false;
				// 如果不想关闭，而单是清除loading状态，需要通过ref手动调用方法
				// this.$refs.uModal1.clearLoading();
            this.redirectToBindCard()


			},
			redirectToBindCard() {
				const hospitalId = '39888';
				const redirectUri = 'http://dpzwx.xztongyong.cn/h5/#/pages/MedicalExamination/Healthcard/Healthcard';
				const bindCardUrl = this.constructBindCardUrl(hospitalId, redirectUri);
				window.location.href = bindCardUrl;
			},
			
			constructBindCardUrl(hospitalId, redirectUri) {
				const baseUrl = "https://h5-health.tengmed.com/h5/tencent/open/card/regist";
				const encodedRedirectUri = encodeURIComponent(redirectUri);
				let bindCardUrl = `${baseUrl}?hospitalId=${hospitalId}&redirect_uri=${encodedRedirectUri}`;
				return bindCardUrl;
			},
			
			getjiuzhen() {
				this.$request("patient", "defaultInfo")
					.then(res => {
						console.log("获取默认就诊人", res)
						if (res.code == 1) {
							this.doctorshow = false
						} else {
							this.doctorshow = true
						}

					});


			},

			change(e) {
				console.log("e:", e);
				this.sousuo.dy = e
				this.getlistpage()
			},
			getMpWxCode(OrderNumber) {
				uni.login({
					success: async resCode => {
						console.log("rescode", resCode)
						let res = await this.$request('auth', 'getopenid', {
							Code: resCode.code
						});

						if (res.code === 1) {
							console.log("res", res)
							this.appid = res.data.appid;
							this.sessionkey = res.data.session_key;
							this.openid = res.data.openid;
							this.pays(OrderNumber)
						}
					}
				})
			},

			pays(OrderNumber) {
				console.log(OrderNumber)
				this.$request("pay", "pay", {
					OrderNo: OrderNumber,
					openid: this.openid,
				}).then(resto => {
					console.log(resto)
					if (resto.code == 1) {
						uni.showLoading({
							title: '请等待'
						})
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: resto.data.Timestamp,
							package: resto.data.Package,
							nonceStr: resto.data.Noncestr,
							signType: resto.data.SignType,
							paySign: resto.data.paySign,
							success: (resshree) => {
								console.log(resshree)

								uni.hideLoading();
								this.recordlist()
							},
							fail: (err) => {
								console.log(err)
								uni.hideLoading();
								this.recordlist()
							}

						})

					}

				})

			},
			changes(index) {

				// console.log(index)
				// this.current = index
				// this.Status = this.list[index].Status
				// console.log("this.list[index].Status", this.Status)
				// this.recordlist()

				this.current = index

				//this.Status = this.list[index].Status
				//console.log("this.list[index].Status", this.Status)
				//this.recordlist()
				console.log(index, "牛逼")
				this.Status = index
				if (index == 0) {
					this.getlis()

				} else if (index == 1) {
					this.getpacs()

				}
			},

			getlis() {

				this.$request("lis", "info", {}).then(res => {
					if (res.code == 1) {
						console.log(this.Status)
						this.orderlist = res.data.resultdata

						if (this.orderlist.length != 0) {
							this.show = true
							// this.month=this.formattedDatemonth(res.data.rows[0].VisitDay)
							// this.day=this.formattedDateday(res.data.rows[0].VisitDay)

						} else {
							this.show = false
						}

						uni.hideLoading();
						console.log(res)

					} else {
						this.show = false
					}

				})

			},



			getpacs() {
				this.$request("pacs", "info", {}).then(res => {
					if (res.code == 1) {

						console.log("调用了")
						this.orderlist = res.data.resultdata
						console.log(this.orderlist)
						if (this.orderlist.length != 0) {

							this.show = true
							// this.month=this.formattedDatemonth(res.data.rows[0].VisitDay)
							// this.day=this.formattedDateday(res.data.rows[0].VisitDay)
						} else {
							this.show = false
						}

						uni.hideLoading();
						console.log(res)

					} else {
						this.show = false

					}

				})
			},





			deletesconfirm() {
				//console.log(this.OrderNumber)
				this.$request("Order", "Delete", {
					OrderNo: this.OrderNumber
				}).then(res => {
					if (res.code == 1) {
						this.showdeletes = false
						this.recordlist()
						console.log(res)
					}
				})
			},
			refundconfirm() {
				console.log(this.OrderNumber)
				this.$request("pay", "refund", {
					OrderNo: this.OrderNumber
				}).then(res => {
					if (res.code == 1) {
						this.showrefund = false
						this.recordlist()
						console.log(res)
					}
				})
			},
			confirm() {
				console.log(this.OrderNumber)
				this.$request("Order", "Cancel", {
					OrderNo: this.OrderNumber
				}).then(res => {
					if (res.code == 1) {
						this.deleteshow = false
						console.log(res)
					}
				})
			},
			removes(OrderNumber) {
				this.OrderNumber = OrderNumber
				this.deleteshow = true
			},
			refund(OrderNumber) {
				this.OrderNumber = OrderNumber
				this.showrefund = true
			},
			deletesd(OrderNumber) {
				this.showdeletes = true
				this.OrderNumber = OrderNumber
			},
			formattedDatemonth(VisitDay) {
				const date = new Date(VisitDay);
				return date.toLocaleDateString('zh-CN', {
					month: 'short'
				}).replace(/\//g, '月');
			},
			formattedDateday(VisitDay) {
				const date = new Date(VisitDay);
				return date.toLocaleDateString('zh-CN', {
					day: 'numeric'
				}).replace(/\//g, '月');
			},
			// recordlist() {
			// 	uni.showLoading({
			// 		title: '加载中'
			// 	})
			// 	this.Status = this.list[this.current].Status
			// 	this.$request("Order", "List", {
			// 		page: this.page,
			// 		rows: this.rows,
			// 		Status: this.Status
			// 	}).then(res => {
			// 		if (res.code == 1) {
			// 			this.orderlist = res.data.rows
			// 			if(this.orderlist.length!=0){
			// 				this.show=true
			// 				this.month=this.formattedDatemonth(res.data.rows[0].VisitDay)
			// 				this.day=this.formattedDateday(res.data.rows[0].VisitDay)
			// 			}else{
			// 				this.show=false
			// 			}

			// 			uni.hideLoading();
			// 			console.log(res)
			// 		}
			// 	})
			// }
		}
	}
</script>

<style>
	@import './newbgcx.css'
</style>